package com.clb.toolsbox.toolsJPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Tool extends JPanel {
    private JTextArea inputArea;
    private JTextArea outputArea;
    private JButton encodeButton;
    private JButton decodeButton;

    public Base64Tool() {
        initializeUI();
    }

    private void initializeUI() {
        setLayout(new BorderLayout(10, 10));

        // 输入区域
        inputArea = new JTextArea(10, 40); // 行数从 5 增加到 10，列数从 30 到 40
        inputArea.setLineWrap(true);
        JScrollPane inputScroll = new JScrollPane(inputArea);
        JLabel inputLabel = new JLabel("输入内容:");

        // 输出区域
        outputArea = new JTextArea(10, 40);
        outputArea.setEditable(false);
        outputArea.setLineWrap(true);
        JScrollPane outputScroll = new JScrollPane(outputArea);
        JLabel outputLabel = new JLabel("转换结果:");

        // 按钮面板
        encodeButton = new JButton("Base64 编码");
        decodeButton = new JButton("Base64 解码");

        encodeButton.addActionListener(this::encode);
        decodeButton.addActionListener(this::decode);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
        buttonPanel.add(encodeButton);
        buttonPanel.add(decodeButton);

        // 组合布局
        JPanel contentPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件之间的间距

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        contentPanel.add(inputLabel, gbc);

        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH; // 允许扩展填充
        gbc.weightx = 1.0;
        gbc.weighty = 0.5;
        contentPanel.add(inputScroll, gbc);

        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.weightx = 0;
        gbc.weighty = 0;
        contentPanel.add(outputLabel, gbc);

        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 0.5;
        contentPanel.add(outputScroll, gbc);

        add(contentPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }


    private void encode(ActionEvent e) {
        String input = inputArea.getText();
        if (!input.isEmpty()) {
            byte[] encodedBytes = Base64.getEncoder().encode(input.getBytes(StandardCharsets.UTF_8));
            String encodedString = new String(encodedBytes, StandardCharsets.UTF_8);
            outputArea.setText(encodedString);
        }
    }

    private void decode(ActionEvent e) {
        String input = inputArea.getText();
        if (!input.isEmpty()) {
            try {
                byte[] decodedBytes = Base64.getDecoder().decode(input.getBytes(StandardCharsets.UTF_8));
                String decodedString = new String(decodedBytes, StandardCharsets.UTF_8);
                outputArea.setText(decodedString);
            } catch (IllegalArgumentException ex) {
                outputArea.setText("无效的 Base64 数据");
            }
        }
    }
}
